<?php
/*
Template Name: Projects Template
*/
?>

<?php get_header(); ?>

<?php the_post(); 

	/*
$args = array(
		'type' => 'post',
		'orderby' => 'id',
		'order' => 'ASC',
		'taxonomy' => 'portfolio',
		'pad_counts' => false );
	
	$categories = get_categories( $args );
	
	print_r($categories);
*/
  //$projects_list = new WP_Query( 'orderby=post_date&order=DESC&post_type=portfolio&post_status=publish&portfolio_category=cat' );
 /*
 $projects_list = get_posts( array( 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'portfolio', 'post_status' => 'publish', 'portfolio_category' => 'Cat' ) );
  print_r($projects_list);

*/

$querystr = "
	SELECT term_id
	FROM $wpdb->term_taxonomy
	WHERE taxonomy = 'du-an'
";

$categories = $wpdb->get_results($querystr, OBJECT);

$catid = 0;

if (!isset($_GET['cat_id'])){
	$catid = $categories[0]->term_id;
} else {
	$catid = $_GET['cat_id'];
}

$cat_effect = get_option("portfolio_transition_effect_$catid");

//echo "catid: ".$catid;


$querystr = "
	SELECT term_taxonomy_id
	FROM $wpdb->term_taxonomy
	WHERE term_id = ".$catid."
";

$pageposts = $wpdb->get_results($querystr, OBJECT);
$term_taxonomy_id = $pageposts[0]->term_taxonomy_id;


$querystr = "
	SELECT object_id
	FROM $wpdb->term_relationships
	WHERE term_taxonomy_id = ".$term_taxonomy_id."
";
$pageposts = $wpdb->get_results($querystr, OBJECT);


$posts = array();
foreach ($pageposts as $pp){
	$id = $pp->object_id;
	$querystr = "
		SELECT $wpdb->posts.* 
		FROM $wpdb->posts
		WHERE $wpdb->posts.ID = $id 
		AND $wpdb->posts.post_status = 'publish'
		ORDER BY $wpdb->posts.post_date DESC
	";
	$result = $wpdb->get_results($querystr, OBJECT);
	array_push($posts, $result);
}

?>

<!-- start Content Home -->				
<div class="content">

<div class="titlepage">
	<p><span><?php the_title(); ?></span></p>
</div>

<div id="projects">

	<!-- Thumbnails projects section -->
	<div class="gallery_projects">
		
		<div id="content_proj" class="content_proj">
			<div class="projectnav">							
				<a href="javascript:;" class="nobackthumbs" ></a>
				<a href="javascript:;" class="project_0 moreinfo"></a>

				<ul class="linkseffects">
					<?php $cat_total = 1; foreach ($categories as $cat) : ?>
					<?php 
						$querystr2 = "
							SELECT term_id, name
							FROM $wpdb->terms
							WHERE term_id = ".$cat->term_id."
						";
						
						$categ = $wpdb->get_results($querystr2, OBJECT);

					?>
					<li><a href="<?php $questionmark = strpos($_SERVER["REQUEST_URI"],"?"); $amper = strpos($_SERVER["REQUEST_URI"],"&"); if ($questionmark && $amper){ $url = substr($_SERVER["REQUEST_URI"], 0, $amper); $url .= '&cat_id=' . $categ[0]->term_id; } if ($questionmark && !$amper){$url = $_SERVER["REQUEST_URI"]. '&cat_id=' . $categ[0]->term_id; } if (!$questionmark){ $url = $_SERVER["REQUEST_URI"]. '&cat_id=' . $categ[0]->term_id; } echo $url;?>" class="leffect <?php if(!empty($_GET['cat_id']) && $_GET['cat_id'] == $categ[0]->term_id) echo 'selected'; else if(empty($_GET['cat_id']) && $cat_total == 1) echo 'selected'; ?>"><?php echo $categ[0]->name; ?></a></li>
					<?php $cat_total++; ?>
					<?php endforeach; ?>
				</ul>

				<span id="css_prev" class="css_prev_nav"></span>					
				<span id="css_next" class="css_next_nav"></span>
			</div>
			<div class="typedividerpr"></div>
			<div class="projects">
				<ul class="tj_gallery">
					<?php $project_total = 1; foreach ($posts as $pl) : ?>
						<?php
							$cust_project_thumb_id = array();
							$cust_project_thumb = '';
                            
                            if(has_post_thumbnail( $pl[0]->ID ))
                            {
                                $cust_project_thumb_id = wp_get_attachment_image_src( get_post_thumbnail_id( $pl[0]->ID ), array(175,167) );
                            }
                            if(!empty($cust_project_thumb_id))
                            {
                                $cust_project_thumb = $cust_project_thumb_id[0];
                            }else
                            {
                                $cust_project_thumb = bloginfo('template_directory') . '/img/no-thumb.png';
                            }
						?>
						<li id="project_<?php echo $project_total; ?>">
                            <a href="<?php echo the_permalink(); ?>">
                                <img src="<?php echo $cust_project_thumb; ?>" width="175" height="167"><span class="descript"/><?php if (strlen($pl[0]->post_title) > 14) { $desc = substr($pl[0]->post_title, 0, 14)."..."; } else {$desc = $pl[0]->post_title;} echo $desc; ?></span></a></li>
					<?php $project_total++; ?>
					<?php endforeach; ?>
				
				</ul>
			</div>
			<div class="typedividerprbottom"></div>					
		</div>
	</div>
	<!-- END Thumbnails projects section -->
	
	<div id="projectmoreinfo" style="display:none;">
	<!-- MoreInfo projects section -->
	<div class="proj_nav" id="proj_navigation">
		<a href="javascript:;" class="backthumbs"></a>
		<a href="javascript:;" class="nomoreinfo"></a>
		
		<div class="projectnav">							
			<a href="javascript:;" class="proj_prev css_prev"></a>
			<span class="css_prev" style="display:none;cursor:pointer;"></span>
			<a href="javascript:;" class="proj_next css_next"></a>
			<span class="css_next" style="display:none;cursor:pointer;"></span>
		</div>
	</div>
	<div class="typedividerpr"></div>
	<div class="openproject">
		<?php $project_total = 1; foreach ($posts as $pl) : ?>
			<?php

				$custom = get_post_custom($pl[0]->ID);
				$p_id = $pl[0]->ID;
	 			$sl_photos = $custom["sl_photos$p_id"][0];
	 			
	 			$cust_project_photos_url = get_post_meta($pl[0]->ID, 'arc_project_photos_url', true);
	 			$cust_project_slider_effect = get_post_meta($pl[0]->ID, 'arc_project_slider_effect', true);
	 			$cust_project_slider_speed = get_post_meta($pl[0]->ID, 'arc_project_slider_speed', true);
	 			$cust_project_slider_controlnav = get_post_meta($pl[0]->ID, 'arc_project_slider_controlnav', true);
	 			$cust_project_slider_directionnav = get_post_meta($pl[0]->ID, 'arc_project_slider_directionnav', true);
	 			
	 			if($cust_project_slider_controlnav == 'yes') $pscn = 'true'; else $pscn = 'false';
	 			if($cust_project_slider_directionnav == 'yes') $psdn = 'true'; else $psdn = 'false';
	 			
				//Explode images URL
				$cust_proj_photos_url = explode("\n", $cust_project_photos_url);
				
				//Full-width image
				$large_image = get_post_meta($pl[0]->ID, 'arc_project_large_image', true);
				
								
			?>
			
				

			
			<!-- START PROJECT <?php echo $project_total; ?> -->
			<div id="divnavproject_<?php echo $project_total; ?>" class="project_content">
				<div class="<?php if($large_image == 'yes') echo 'slideprojectsnew'; else echo 'slideprojects'; ?> theme-default">
					<!-- PROJECT <?php echo $project_total; ?> SLIDER -->
					<div id="slider<?php echo $project_total; ?>" class="nivoSlider" style="<?php if($large_image == 'yes') echo 'height:294px'; else echo 'height:350px'; ?>">
		 						<?php for($i=0; $i<count($cust_proj_photos_url); $i++){ 
		 										if($i < count($cust_proj_photos_url) - 1)
													$cust_src = substr($cust_proj_photos_url[$i], 0, -1);
												else
													$cust_src = $cust_proj_photos_url[$i];
		 						?>
									<img src="<?php echo $cust_src; ?>" alt="" width="<?php if($large_image == 'yes') echo '763px'; else echo '423px'; ?>" height="<?php if($large_image == 'yes') echo '294px'; else echo '350px'; ?>">
								<?php } ?>
							
					</div>
					<!-- END PROJECT <?php echo $project_total; ?> SLIDER -->
					
				
					
				</div>
			<div class="projectinfo">
				<?php if($large_image != 'yes'){ ?>
				<div class="typedivider"></div>
				<?php } ?>
				<div class="<?php if($large_image == 'yes') echo 'titlenew'; else echo 'title'; ?>">
					<h2><?php echo $pl[0]->post_title; ?></h2>
				</div>
				<?php if($large_image != 'yes'){ ?>
				<div class="typedivider"></div>
				<?php } ?>
				<div class="<?php if($large_image == 'yes') echo 'infonew'; else echo 'info'; ?>">
					<?php echo do_shortcode($pl[0]->post_content); ?>								
				</div>
				<?php if($large_image != 'yes'){ ?>
				<div class="typedivider"></div>
				<?php } ?>
			</div>
		</div>
	<!-- END PROJECT <?php echo $project_total; ?> -->
	
	<script type="text/javascript">
					/* START NIVO SLIDER */
					$(window).load(function() {
						$('#slider<?php echo $project_total; ?>').nivoSlider({
					        effect: '<?php echo $cust_project_slider_effect; ?>', // Specify sets like: 'fold,fade,sliceDown'
					        slices: 15, // For slice animations
					        animSpeed: 1000, // Slide transition speed
					        pauseTime: '<?php echo $cust_project_slider_speed; ?>', // How long each slide will show
					        startSlide: 0, // Set starting Slide (0 index)
					        directionNav: <?php echo $psdn; ?>, // Next & Prev navigation
					        directionNavHide: true, // Only show on hover
					        controlNav: <?php echo $pscn; ?>, // 1,2,3... navigation
					        controlNavThumbs: false, // Use thumbnails for Control Nav
					        controlNavThumbsFromRel: false, // Use image rel for thumbs
					        keyboardNav: true, // Use left & right arrows
					        pauseOnHover: true, // Stop animation while hovering
					        manualAdvance: false, // Force manual transitions
					        captionOpacity: false, // Universal caption opacity
					        prevText: false, // Prev directionNav text
					        nextText: false, // Next directionNav text
							  });
				        
				    });
				    /*END NIVO SLIDER*/
				</script>
		<?php $project_total++; ?>
		<?php endforeach; ?>		
</div>
<!-- END MoreInfo projects section -->
<div class="typedividerprbottom"></div>
</div>
	
	
<script type="text/javascript">
			$(function() {
				$('#content_proj').gridnav({
					rows	: 2,
					navL	: '#css_prev',
					navR	: '#css_next',
					type	: {
						mode		: '<?php if(!empty($cat_effect['portfolio_transition_effect'])) echo $cat_effect['portfolio_transition_effect']; else echo "fade"; ?>', 		// use def | fade | seqfade | updown | sequpdown | showhide | disperse | rows
						speed		: <?php if($cat_effect['portfolio_transition_effect'] == 'rows') echo "1000"; else echo "500"; ?>,			// for fade, seqfade, updown, sequpdown, showhide, disperse, rows
						easing		: '<?php if($cat_effect['portfolio_transition_effect'] == 'rows') echo "easeInOutBack"; ?>',			// for fade, seqfade, updown, sequpdown, showhide, disperse, rows	
						factor		: <?php if($cat_effect['portfolio_transition_effect'] == 'rows') echo "150"; else if($cat_effect['portfolio_transition_effect'] == 'seqfade') echo "100"; else echo "''"; ?>,			// for seqfade, sequpdown, rows
						reverse		: ''			// for sequpdown
					}
				});
			});
	</script>
	

</div>

		<div class="hidecorner"></div>
		
		</div>	
	</div>	
</div>

<?php get_footer(); ?>